package com.meiqiongproperty.yiguanjia.fragment.home.health;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.commom.ViewConstant;
import com.meiqiongproperty.yiguanjia.databinding.HealthyShopConfirmOrderLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.yiHealthyMedicalAdapter.HealthyShopConfirmOrderListAdapter;
import com.meiqiongproperty.yiguanjia.fragment.home.shopping.bean.ShopLocalListBean;
import com.meiqiongproperty.yiguanjia.fragment.pay.PayTypeFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendOrderData;
import com.meiqiongproperty.yiguanjia.httpbean.shopping.ConfirmOrderProduct;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayWX;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayZFB;
import com.meiqiongproperty.yiguanjia.ui.dialog.FragmentExchangeController;
import com.meiqiongproperty.yiguanjia.ui.picker.DataPickerDialog;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/21.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.health
 */

public class HealthyConfirmOrderFragment extends BaseFragment {
    HealthyShopConfirmOrderLayoutBinding binding;
    HealthyShopConfirmOrderListAdapter adapter;
    Dialog chooseDialog;
    List<String> diliverTimeList=new ArrayList<>();
    String deliverType = "1";
    int type;
    double totalPrice = 0.0;
    List<ConfirmOrderProduct> orderList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.healthy_shop_confirm_order_layout, container, false);
        binding.setClick(this);

        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.navigation.setTitle("确认订单");

        for (int i = 0; i < ViewConstant.list_local_shop.size(); i++) {
            ShopLocalListBean bean = ViewConstant.list_local_shop.get(i);
            ConfirmOrderProduct product = new ConfirmOrderProduct();
            product.setProduct_amount(bean.getProduct_count() + "");
            product.setProduct_name(bean.getProduct_title());
            product.setProduct_per_price("￥" + bean.getProduct_oprice());
            double price = bean.getProduct_price() * bean.getProduct_count();
            totalPrice += price;
            product.setProduct_total_price("￥" + String.valueOf(price));
            orderList.add(product);
        }

        diliverTimeList.add("工作日及双休日送货");
        diliverTimeList.add("仅工作日送货");
        diliverTimeList.add("仅双休日送货");

        adapter=new HealthyShopConfirmOrderListAdapter(getActivity(),ViewConstant.list_local_shop);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.confirmHealthyOrderList.setLayoutManager(manager);
        binding.confirmHealthyOrderList.setAdapter(adapter);

        binding.orderTotalPrice.setText("￥" + totalPrice);
        binding.tvConsignee.setText("收货人：" + Utils.getUserRealName(getActivity()));
        binding.tvPhone.setText("电话：" + Utils.getUserPhoneNumber(getActivity()));
        binding.tvAddress.setText("地址：" + Utils.getUserAddress(getActivity()));
        return binding.getRoot();
    }

    public void deliverTimeClick(View view) {
        if (Utils.isFastDoubleClick())return;
        showChooseDialog(diliverTimeList);
    }

    private void showChooseDialog(List<String> mlist) {
        final DataPickerDialog.Builder builder = new DataPickerDialog.Builder(getActivity());
        chooseDialog = builder.setData(mlist).setSelection(1).setTitle("取消")
                .setOnDataSelectedListener(new DataPickerDialog.OnDataSelectedListener() {
                    @Override
                    public void onDataSelected(String itemValue, int position) {
                        deliverType=String.valueOf(position+1);
                        binding.tvDeliveryTime.setText(itemValue);
                    }

                    @Override
                    public void onCancel() {

                    }
                }).create();
        chooseDialog.show();
    }

    /**
     * 选择支付方式
     */
    public void select_pay_type(View view){
        PayTypeFragment payTypeSeleor = new PayTypeFragment();
        payTypeSeleor.setPayTypeClickListener(new PayTypeFragment.PayTypeClickListener() {
            @Override
            public void setData(Object data) {
                type= (int) data;
                switch (type){
                    case 1:
                        binding.payType.setText("支付宝支付");
                        break;
                    case 2:
                        binding.payType.setText("微信支付");
                        break;
                }
            }
        });
        FragmentExchangeController.addFragment(getFragmentManager(), payTypeSeleor, payTypeSeleor.getTagName());
    }

    /**
     * 提交订单并购买
     */
    public void submitOrderClick(View view){
        if (Utils.isFastDoubleClick()){
            return;
        }
        sumitOrderData();
    }

    /**
     * 提交订单数据
     */
    public void sumitOrderData() {
        SendOrderData bean=new SendOrderData();
        bean.setToken(Utils.getToken(getActivity()));
        bean.setRoomID(Utils.getRoomID(getActivity()));
        bean.setDeliverType(deliverType);
        bean.setPayMethod(type);
        Gson gson=new Gson();
        String cartJson=gson.toJson(ViewConstant.list_local_shop);
        bean.setCartJsonInfo(cartJson);
        switch (type){
            case 1:
                ApiService.submitOrderDataZFB(getActivity(), new SimpleHttpCallback<CommPayZFB>() {
                    @Override
                    public void onNext(CommPayZFB commPayZFB) {
                        String orderInfo = commPayZFB.getParameter().getParameter();
                        doAlipay(orderInfo);
                    }
                }, Utils.getRequestBody(bean));
                break;
            case 2:
                ApiService.submitOrderDataWX(getActivity(), new SimpleHttpCallback<CommPayWX>() {
                    @Override
                    public void onNext(CommPayWX commPayWX) {
                        doWXPay(commPayWX.getParameter().getParameter(),commPayWX.getParameter().getParameter().getAppid());
                    }
                }, Utils.getRequestBody(bean));
                break;
        }
    }
}
